// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-07-23

package conf

import (
	"gitee.com/go-libs/config"
	"os"
	"time"
)

// Config
// is a singleton instance.
var Config *Configuration

const (
	defaultConfigurationHost    = "0.0.0.0"
	defaultConfigurationPort    = 8080
	defaultConfigurationName    = "example"
	defaultConfigurationVersion = "0.0.0"
)

// Configuration
// is a config component for framework.
type Configuration struct {
	Host    string `yaml:"host"`
	Port    int    `yaml:"port"`
	Name    string `yaml:"name"`
	Version string `yaml:"version"`

	// Enable debugger.
	DebugProf       bool   `yaml:"debug-prof"`
	DebugProfPrefix string `yaml:"debug-prof-prefix"`

	// Pid
	// runtime process id of os.
	Pid     int       `yaml:"-"`
	Started time.Time `yaml:"-"`

	err error
}

func (o *Configuration) After() {
	if o.Host == "" {
		o.Host = defaultConfigurationHost
	}
	if o.Port == 0 {
		o.Port = defaultConfigurationPort
	}
	if o.Name == "" {
		o.Name = defaultConfigurationName
	}
	if o.Version == "" {
		o.Version = defaultConfigurationVersion
	}

	if o.DebugProfPrefix == "" {
		o.DebugProfPrefix = "/debug/prof"
	}
}

// +---------------------------------------------------------------------------+
// | Access methods                                                            |
// +---------------------------------------------------------------------------+

func (o *Configuration) init() *Configuration {
	o.err = config.Seek("app.yml", "app.yaml").ScanYaml(o)

	o.Pid = os.Getpid()
	o.Started = time.Now()
	return o
}
